<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth',[
            'except'=>['index','show','create','store']
        ]);
        $this->middleware('guest',[
            'only'=>['create','store']
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users=User::paginate(10);
        return view('user.list',compact('users'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=$this->validate($request,[
            'name'=>'required|min:3|max:8',
            'email'=>'required|email|unique:users',
            'password'=>'required|min:5|confirmed',
        ]);
        $data['password']=bcrypt($data['password']);
        User::create($data);
        $status=\Auth::attempt(['email'=>$request->input('email'),'password'=>$request->input('password')]);
        if ($status){
            session()->flash('success','注册成功,已经为你自动登陆');
            return redirect()->route('home');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param User $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        $blogs=$user->blogs()->orderBy('id','DESC')->paginate(10);
        if (\Auth::check())
        $follower=$user->isFollow(\Auth::user()->id)?'取消关注':'关注';
        return view('user.show',compact('user','blogs','follower'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param User $user
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function edit(User $user)
    {
        $this->authorize('update',$user);
        return view('user.edit',compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {
        $this->validate($request,[
            'name'=>'required|min:3|max:8',
            'email'=>[Rule::unique('users')->ignoreModel($user),'required','email'],
            'password'=>'nullable|min:5|confirmed',
        ]);
        if ($request->input('password')){
            $user->password=bcrypt($request->input('password'));
        }
        $user->name=$request->input('name');
        $user->email=$request->input('email');
        $user->save();
        return redirect()->route('user.show',$user);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    public function follower(User $user){
        $user->followToggle(\Auth::user()->id);
        return back();
    }
}
